<?php 
namespace Admin\Controller;
use Think\Controller;

class FriendsController extends AdminController
{
	public function index()
	{
		$data=D('friends')->select();
		$this->assign('list',$data);
		$this->display();	
	}

	public function add()
	{
		$this->display();	
	}

    public function doadd()
    {
    	$friends=D('friends');
    	if (!$friends->create()) {
            $this->error($friends->getError());
        } else {
            if ($friends->add() > 0) {
                $this->success('添加成功',U('index'));
            } else {
                $this->error('添加失败,可能是url地址不正确');
            }
        }
    }


    //编辑页
    public function edit($id)
    {
        //接收ID
        $id = I('get.id/d');
        //查找单条数据
        $data = M('friends')->find($id);
        $this->assign('data',$data);
        $this->display('friends/edit');
    }

    //执行修改
    public function update()
    {
        if (empty($_POST)) {
            $this->error('请填写数据!',U('Friends/edit'));
            exit;
        }
        D('friends')->create();
        if (D('friends')->save()) {
            $this->success('修改成功',U('index'));
        } else {
            $this->error('修改失败,可能是url地址不正确');
        }
    }    

    public function del()
    {
        if (empty($_GET['id'])) {
            $this->redirect('Friends/index');
            exit;
        }
        $id = I('get.id/d');
    
        M('friends')->delete($id);
    }

	public function status()
	{
		$id =  I('get.id/d');
		$data['status'] = $_GET['s'];
		$friends = M('friends');
		$friends->where(array('id'=>$id))->save($data);
        $this->redirect('index');
	}

    public function search()
    {
        $name=I('post.name');
        $friends=D('friends');
        $where['name']=array('like','%'.$name.'%');
        $list=$friends->where($where)->select();
        $this->assign('list',$list);
        $this->display('Friends/index');
    }
}